package com.dubito.encrypt.sha;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;

/**
 * @author liliang
 * @description:
 * @date 2018/5/17.
 */
public class SecHmac {

    public static byte[] hmac(String hmacAlgorithm, SecretKey key, byte[] input) throws Exception {
        Mac mac = Mac.getInstance(hmacAlgorithm);
        mac.init(key);
        mac.update(input);
        return mac.doFinal();
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String algorithm = "HmacSHA1";
        String data = "helloworld";
        KeyGenerator keygen = KeyGenerator.getInstance(algorithm);
        SecretKey sKey = keygen.generateKey();
        byte[] key = sKey.getEncoded();
        System.out.println(String.format("%032x", new BigInteger(1, key)));
    }
}
